package com.hue.ps.direct;

import com.hue.utils.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/*
    发布与订阅模式之direct：消费者2
 */
public class Customer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //2.获取连接
        Connection connection = ConnectionUtils.getConnection();
        //3.获取频道
        Channel channel = connection.createChannel();

        //4.声明交换机
        channel.exchangeDeclare("direct_exchange",BuiltinExchangeType.DIRECT);
        //5.声明队列
        channel.queueDeclare("direct_queue2", true, false, false, null);
        //6.将队列绑定到交换机上
        channel.queueBind("direct_queue2","direct_exchange","key2");

        //7.创建消费者来处理消息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("customer2接收到的消息："+new String(body));
            }
        };

        //7.监听队列
        channel.basicConsume("direct_queue2", true,consumer);
    }
}
